<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/6/5
 * Time: 17:38
 */

namespace App\Http\View\Composers;

use Illuminate\Support\Facades\Redis;
use Illuminate\View\View;

class LayoutsHeaderComposer
{
    /**
     * Bind data to the view.
     *
     * @param View $view
     *
     * @return void
     */
    public function compose(View $view)
    {
        $headData = $this->_getHeadData();
        $view->with('common', $headData);
    }

    private function _getHeadData()
    {
        $data = Redis::get('headData');
        if (!$data) {
            $result = [];

            $url = env('QINIUURL');

            $info = Redis::get('Info');
            if ($info) {
                $info                                 = json_decode($info, true);
                $result['contact']['contactUs']       = '联系我们';
                $result['contact']['name']            = $info['title']; //公司名称
                $result['contact']['corporation']     = $info['address']; //公司地址
                $result['contact']['phone']           = $info['phone']; //联系电话
                $result['contact']['mailbox']         = $info['mailbox'] ?? ''; //邮箱
                $result['contact']['company_profile'] = $info['company_profile'] ?? ''; //公司简介
                $result['contact']['image']           = $url . '/' . $info['image']; // 联系我们图片
                $result['contact']['nav_image']       = $url . '/' . $info['nav_image']; // 导航图片
                $result['contact']['menu_image']      = $url . '/' . $info['menu_image']; // 导航图片
                $result['contact']['wechat_code']     = $url . '/' . $info['wechat_code']; // 导航图片
            } else {
                $result['contact'] = [
                    'contactUs'       => '联系我们',
                    'name'            => '广州风行者文化传媒有限公司',
                    'corporation'     => '广东省广州市番禺区繁华路1号友谊中心',
                    'phone'           => '广东省广州市番禺区繁华路1号友谊中心',
                    'mailbox'         => '广东省广州市番禺区繁华路1号友谊中心',
                    'company_profile' => '广东省广州市番禺区繁华路1号友谊中心',
                    'image'           => '',
                    'nav_image'       => '',
                    'menu_image'      => '',
                    'wechat_code'     => ''
                ];
            }

            $res = json_encode($result);
            Redis::setex('headData', 36000, $res);

            return $result;
        }
        $data = json_decode($data, true);

        return $data;
    }
}
